package com.kg.module.jtnc.hhrdagl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.core.annotation.AutoOperateLog;
import com.kg.core.annotation.NoRepeatSubmit;
import com.kg.core.exception.BaseException;
import com.kg.core.web.ResponseResult;
import com.kg.module.jtnc.hhrdagl.dto.KrHhrdaglDTO;
import com.kg.module.jtnc.hhrdagl.dto.convert.KrHhrdaglConvert;
import com.kg.module.jtnc.hhrdagl.entity.KrHhrdagl;
import com.kg.module.jtnc.hhrdagl.service.KrHhrdaglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 合伙人档案管理表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-02
 */
@RestController
@RequestMapping("/jtnc/hhrdagl/krHhrdagl")
@Api(tags = "合伙人档案管理表", value = "合伙人档案管理表", description = "KrHhrdaglController")
public class KrHhrdaglController {

    @Resource
    private KrHhrdaglService krHhrdaglService;
    @Resource
    private KrHhrdaglConvert krHhrdaglConvert;

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/select", value = "合伙人下拉", httpMethod = "GET")
    @GetMapping("/select")
    public List<KrHhrdagl> select() {
        List<KrHhrdagl> list = krHhrdaglService.lambdaQuery().orderByDesc(KrHhrdagl::getCreateTime).list();
        return list;
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/getById", value = "详情-合伙人档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hhrId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getById")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:getById')")
    public KrHhrdaglDTO getById(String hhrId) {
        return krHhrdaglConvert.entityToDto(krHhrdaglService.getById(hhrId));
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/list", value = "分页列表-合伙人档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:list')")
    public Page<KrHhrdaglDTO> list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                               @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                               @RequestParam(value = "params", required = false) String params) {
        return krHhrdaglService.pagelist(page, limit, params);
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/add", value = "新增-合伙人档案管理表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:add')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/hhrdagl/krHhrdagl/add", logMsg = "新增-合伙人档案管理表")
    public void add(@RequestBody KrHhrdaglDTO krHhrdaglDTO) throws BaseException {
        try {
            krHhrdaglService.add(krHhrdaglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/update", value = "修改-合伙人档案管理表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:update')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/hhrdagl/krHhrdagl/update", logMsg = "修改-合伙人档案管理表")
    public void update(@RequestBody KrHhrdaglDTO krHhrdaglDTO) throws BaseException {
        try {
            krHhrdaglService.update(krHhrdaglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "修改失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/delete", value = "删除-合伙人档案管理表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hhrIds", value = "待删除id列表", paramType = "body", required = true, allowMultiple = true, dataType = "String")
    })
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:delete')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/hhrdagl/krHhrdagl/delete", logMsg = "删除-合伙人档案管理表")
    public void delete(@RequestBody String[] hhrIds) throws BaseException {
        try {
            krHhrdaglService.delete(Arrays.asList(hhrIds));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "删除失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/export/excel", value = "导出excel-合伙人档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/export/excel")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:export:excel')")
    public String exportExcel(@RequestParam(value = "params", required = false) String params) throws BaseException {
        String result = krHhrdaglService.exportExcel(params);
        if ("error".equals(result)) {
            throw new BaseException("导出Excel失败，请重试！");
        }
        return result;
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/import/excel", value = "导入excel-合伙人档案管理表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", paramType = "query", required = false, dataType = "HttpServletRequest")
    })
    @PostMapping("/import/excel")
    @PreAuthorize("hasAuthority('jtnc:hhrdagl:krHhrdagl:import:excel')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/hhrdagl/krHhrdagl/import/excel", logMsg = "导入excel-合伙人档案管理表")
    public ResponseResult importExcel(HttpServletRequest request) throws BaseException {
        try {
            String result = krHhrdaglService.importExcel(request);
            if (StringUtils.hasText(result)) {
                // 导入失败，返回错误提示信息
                return ResponseResult.builder().code("200").message(result).build();
            } else {
                return ResponseResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "导入Excel失败，请重试！");
        }
    }

    @ApiOperation(notes = "/jtnc/hhrdagl/krHhrdagl/import/downloadTemplate", value = "下载导入模板-合伙人档案管理表", httpMethod = "GET")
    @GetMapping("/import/downloadTemplate")
    public String downloadTemplate() throws BaseException {
        String result = krHhrdaglService.downloadTemplate();
        if ("error".equals(result)) {
            throw new BaseException("下载导入模板失败，请重试！");
        }
        return result;
    }
}
